package admin

import (
	"gitee.com/gopher2011/gin"
	"gitee.com/gopher2011/mall/app/dao"
	"gitee.com/gopher2011/mall/app/service"
	"net/http"
	"strconv"
)

// 后台资源分类管理 /resourceCategory
var UmsResourceCategory = &umsResourceCategoryApi{}

type umsResourceCategoryApi struct{}

// 查询所有后台资源分类 /listAll Get 参数: 无
func (it *umsResourceCategoryApi) ListAll(r *gin.Context) {
	res, err := service.UmsResourceCategory.ListAll()
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "查询所有后台资源分类失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "查询所有后台资源分类成功!", "data": res})
	}
}

// 添加后台资源分类 /create Post 参数: dao.UmsResourceCategory
func (it *umsResourceCategoryApi) Create(r *gin.Context) {
	var p dao.UmsResourceCategory
	r.Parse(&p)
	res, err := service.UmsResourceCategory.Create(&p)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "添加后台资源分类失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "添加后台资源分类成功!", "data": res})
	}
}

// 修改后台资源分类 /update/:id Post 参数:id,dao.UmsResourceCategory
func (it *umsResourceCategoryApi) Update(r *gin.Context) {
	str := r.Param("id")
	id, _ := strconv.Atoi(str)
	var p dao.UmsResourceCategory
	r.Parse(&p)
	res, err := service.UmsResourceCategory.Update(int64(id), &p)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "修改后台资源分类失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "修改后台资源分类成功!", "data": res})
	}
}

// 根据ID删除后台资源 /delete/:id Post 参数:id
func (it *umsResourceCategoryApi) Delete(r *gin.Context) {
	str := r.Param("id")
	id, _ := strconv.Atoi(str)
	res, err := service.UmsResourceCategory.Delete(int64(id))
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "根据ID删除后台资源失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "根据ID删除后台资源成功!", "data": res})
	}
}
